﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Member
{
    public class MemberBL
    {
        protected string clsMemberInsert(string loginid, string password)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                string result = "";
                var verify = from n in adlerContextDb.tbl_Members
                             where n.LoginId == loginid
                             select n.IsVerified;
                bool checkverify = Convert.ToBoolean(verify);
                if (checkverify)
                    result = "1";
                else
                {
                    var getloginid = from n in adlerContextDb.tbl_Members
                                 where n.LoginId == loginid && n.Password == password && n.Status == "NORMAL"
                                 select n.LoginId;
                    result = getloginid.FirstOrDefault();
                }
                return result;
            }
        }

        public void clsMemberInsert(tbl_Member members)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Member member = new tbl_Member();
                    member.LoginId = members.LoginId;
                    member.MemberName = members.MemberName;
                    member.MemberGuid = members.MemberGuid;
                    member.Password = members.Password;
                    member.Gender = members.Gender;
                    member.DateBone = members.DateBone;
                    member.Email = members.Email;
                    member.Phone = members.Phone;
                    member.IsMarketEmail = members.IsMarketEmail;
                    member.Address = members.Address;
                    member.Postcode = members.Postcode;
                    member.PostCodeName = members.PostCodeName;
                    member.CouponCount = 0;
                    member.VisitCount = 1;
                    member.OrderTotal = 0;
                    member.MemberType = members.MemberType;
                    member.RegisterDate = DateTime.Now;
                    member.RegisterPath = members.RegisterPath;
                    member.AdminNote = members.AdminNote;
                    member.BankAccount = members.BankAccount;
                    member.BankName = members.BankName;
                    member.IsVerified = members.IsVerified;
                    member.VerifyCode = members.VerifyCode;
                    member.Status = members.Status;

                    adlerContextDb.tbl_Members.InsertOnSubmit(member);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsMemberVisit(string loginid)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var member = adlerContextDb.tbl_Members.Single(d => d.LoginId == loginid);
                    member.LoginId = loginid;
                    member.LastVisitDate = DateTime.Now;
                    member.VisitCount = member.VisitCount + 1;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsMemberUpdateStatus(string loginid, string status)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var member = adlerContextDb.tbl_Members.Single(d => d.LoginId == loginid);
                    member.Status = status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Emails.
        public List<MODEL_SP_ADMIN_SEARCH_MEMBER> Get_Admin_Members(string membername, string gender, string email, string phone, string membertype, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_MEMBER(membername, gender, email, phone, membertype, status)
                //            select new MODEL_SP_ADMIN_SEARCH_MEMBER
                //            {
                //                Index = index++,
                //                MemberNo = n.MemberNo,
                //                LoginId = n.LoginId,
                //                MemberName = n.MemberName,
                //                MemberGuid = n.MemberGuid,
                //                Password = n.Password,
                //                Gender = n.Gender,
                //                DateBone = n.DateBone,
                //                Email = n.Email,
                //                Phone = n.Phone,
                //                IsMarketEmail = n.IsMarketEmail,
                //                Address = n.Address,
                //                Postcode = n.Postcode,
                //                PostCodeName = n.PostCodeName,
                //                CouponCount = n.CouponCount,
                //                VisitCount = n.VisitCount,
                //                OrderTotal = n.OrderTotal,
                //                MemberType = n.MemberType,
                //                RegisterDate = n.RegisterDate,
                //                LastVisitDate = n.LastVisitDate,
                //                RegisterPath = n.RegisterPath,
                //                AdminNote = n.AdminNote,
                //                BankAccount = n.BankAccount,
                //                BankName = n.BankName,
                //                IsVerified = n.IsVerified,
                //                VerifyCode = n.VerifyCode,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_Member GetMemberByMemberno(int memberno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Members.Single(d => d.MemberNo == memberno);
            }
        }

        public string checkLogin(string email, string password)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                //EncryptionManager encrypt = new EncryptionManager();
                //password = encrypt.Encrypt(password);
                var username = from n in contextdb.tbl_Members
                               where n.Email == email && n.Password == password && n.IsVerified == true && n.Status == "NORMAL"
                               select n.MemberName;
                return username.FirstOrDefault();
            }
        }

        public bool checkExistsEmail(string email) 
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                bool result = true;
                var query = from n in contextdb.tbl_Members
                            where n.Email == email && n.Status == "NORMAL"
                            select n.Email;
                if (query.FirstOrDefault() != null && query.FirstOrDefault() != "")
                    result = false;

                return result;
            }
        }

        public bool checkExistsPhone(string phone)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                bool result = true;
                var query = from n in contextdb.tbl_Members
                            where n.Phone == phone && n.Status == "NORMAL"
                            select n.Phone;
                if (query.FirstOrDefault() != null && query.FirstOrDefault() != "")
                    result = false;

                return result;
            }
        }
    }
}
